using DG.Tweening;
using UnityEngine;

namespace Share.UI
{
    //节点飞进来，停留一会，在飞走的效果
    public class UIEffect_NodeMoveInOut : UIEffectBase
    {
        [SerializeField]
        [Header("进出的移动时间")]
        float m_MoveDuration = 0.5f;
        [SerializeField]
        [Header("停留时间")]
        float m_StayTime = 0.5f;
        [SerializeField]
        public Vector2 m_MoveOffset = new Vector2(0, 0);

        private Vector2 m_OriginalPosition;
        private RectTransform m_RectTransform;

        // Start is called before the first frame update
        protected override void OnEnable()
        {

            base.OnEnable();
        }

        // Update is called once per frame
        void Update()
        {
        }

        public override void OnActionEnter()
        {
            if (m_RectTransform == null)
                m_RectTransform = GetComponent<RectTransform>();

            m_OriginalPosition = m_RectTransform.anchoredPosition;

            // 设置初始位置为偏移位置
            Vector2 startPos = m_OriginalPosition + m_MoveOffset;
            m_RectTransform.anchoredPosition = startPos;

            // 动画到当前位置（原始位置）
            m_RectTransform.DOAnchorPos(m_OriginalPosition, m_MoveDuration)
                .SetEase(Ease.InQuad)
                .OnComplete(() =>
                {
                    // 停留一段时间后飞回去
                    DOVirtual.DelayedCall(m_StayTime, () =>
                    {
                        m_RectTransform.DOAnchorPos(startPos, m_MoveDuration)
                            .SetEase(Ease.OutQuad).OnComplete(() => gameObject.SetActive(false));
                    });
                });
        }

    }
}
